package hander

import (
	"github.com/gin-gonic/gin"
	"good/api/reponse"
	"good/api/request"
	"good/global"
	"good/internal/dao"
	"good/internal/models"
	"good/pkg"
	"time"
)

func Login(c *gin.Context) {
	req := request.Login{}
	if err := c.ShouldBind(&req); err != nil {
		reponse.ErrorRequest(c, "绑定失败")
		return
	}
	user := models.User{}
	get := global.Rdb.Get(global.Ctx, "sendSms"+req.Phone)
	if get.Val() != req.SendSms {
		reponse.ErrorRequest(c, "短信验证码错误")
		return
	}
	if err := dao.Login(&user, req.Phone); err != nil {
		reponse.ErrorRequest(c, "查询失败")
		return
	}
	global.Rdb.Get(global.Ctx, "token")
	token, err := pkg.Token(int(user.ID))
	if err != nil {
		return
	}
	global.Rdb.Set(global.Ctx, "token", token, time.Minute*5)
	reponse.SuccessRequest(c, "登录成功", token)
}
